/*
Dofile to analyze results of counterfactuals

Author: GA
Last Modified: 8/3/2019

*/

*****Step 1: Reading in a bunch of data**********
****read in base simulated data
import delimited "$dir/Model/Simulated_data/simulated_data_base.csv", clear

//rename
ren v1 state_born
ren v2 state_move
drop v3 v4
ren v5 parent_marr
ren v6 kid_marr
ren v7 parent_time_inv
ren v8 kid_earn_p2
ren v9 kid_earn_p3
ren v10 kid_hc_p3
ren v11 ability
ren v12 coll_par
ren v13 coll_kid
ren v14 weight
ren v15 mig
ren v16 type
ren v17 mig2
ren v18 lp_norec
ren v19 state_p2
ren v20 race
ren v21 region
ren v22 util
ren v23 parent_fam_earn
ren v24 kid_fam_earn
ren v25 parent_pctile
ren v26 kid_pctile

gen iim = kid_pctile if parent_pctile<50

levelsof state_born, clean local(fips)

collapse (mean) kid_fam_earn iim util, by(state_born)
save "$temp/earnings_base", replace

local counter 0
foreach fip in `fips'{
    local counter `++counter'
	import delimited "$dir/Model/Simulated_data/simulated_data_cfact_shock_`counter'.csv", clear
	
	
	
	ren v1 state_born
	ren v2 state_move
	ren v3 kid_marr
	ren v4 kid_earn_p2
	ren v5 l2
	ren v6 kid_earn_p3
	ren v7 coll_kid
	ren v8 weight
	ren v9 util
	ren v10 parent_fam_earn
	ren v11 kid_fam_earn
	ren v12 parent_pctile
	ren v13 kid_pctile
	
	keep if state_born == `fip'
	
	
	gen iim_cfact = kid_pctile if parent_pctile<50

	
	collapse (mean) kid_fam_earn iim util
	ren util util_cfact
	gen state_born = `fip'
	save "$temp/earnings_cfact_wage_shock_`fip'", replace	
}

clear
foreach fip in `fips'{
    append using "$temp/earnings_cfact_wage_shock_`fip'"
}
ren kid_fam_earn kid_fam_earn_cfact
save "$temp/earnings_cfact_shocks", replace

merge 1:1 state_born using "$temp/earnings_base", keep(match) nogen
gen diff = kid_fam_earn_cfact - kid_fam_earn
su diff
gen diff_iim = iim_cfact - iim
gen diff_util = util_cfact - util

ren state_born statefips
merge 1:1 statefips using "$data/Crosswalks/state_fips_crosswalk", keep(match) nogen
maptile diff, geo(state) rev n(7) spopt(mos(thick)) legdecimals(2) ///
savegraph("$output/fig6_cfact_shocks_skill.png") replace


maptile diff_iim, geo(state) rev n(7) spopt(mos(thick)) legdecimals(2) ///
savegraph("$output/fig6_cfact_shocks_skill_iim.png") replace

maptile diff_util, geo(state) rev n(7) spopt(mos(thick)) legdecimals(2) ///
savegraph("$output/fig6_cfact_shocks_skill_util.png") replace

graph close

ren statefips stfips
merge m:1 stfips using "$data/Crosswalks/state_div_crosswalk", keep(match) nogen

local divs `"NE MA ENC WNC SA ESC WSC MO PA"'
local counter 0

qui{
foreach div in `divs'{
	local counter `++counter'
	su diff if division == `counter'
	local report = round(`r(mean)', .001)
	noi di as result "`div' & `report' \\"
}
}


save "$temp/cfact_results_shocks", replace




****************************************************************
****************PART 2: Population Elasticityv
****************************************************************

//state populations in baseline simulation
import delimited "$dir/Model/Simulated_data/simulated_data_base.csv", clear

//            line = [fips[l] fips[lp] skill_prices[l] prim_2010.skill_prices[lp] m mp t e2p e3p h3p_val ap S_par S pops[l] mig e3_fam e3p_fam]


//rename
ren v1 state_born
ren v2 state_move
drop v3 v4
ren v5 parent_marr
ren v6 kid_marr
ren v7 parent_time_inv
ren v8 kid_earn_p2
ren v9 kid_earn_p3
ren v10 kid_hc_p3
ren v11 ability
ren v12 coll_par
ren v13 coll_kid
ren v14 weight
ren v15 mig
ren v16 type
ren v17 mig2
ren v18 lp_norec
ren v19 state_p2
ren v20 race
ren v21 region
ren v22 util
ren v23 parent_fam_earn
ren v24 kid_fam_earn
ren v25 parent_pctile
ren v26 kid_pctile

replace weight = weight*1000000
gen popweight = weight/5000

//in-migration rate
gen count = 1 //total population
gen rawcount = 1/popweight
replace popweight = round(popweight)
collapse (sum) count rawcount [fw=popweight], by(state_move)
//keep count rawcount
save "$temp/pops_baseline", replace


forval i = 1/50{
		import delimited "$dir/Model/Simulated_data/simulated_data_cfact_shock_`i'.csv", clear
	
	ren v1 state_born
	ren v2 state_move
	ren v3 kid_marr
	ren v4 kid_earn_p2
	ren v5 l2
	ren v6 kid_earn_p3
	ren v7 coll_kid
	ren v8 weight
	ren v9 util
	ren v10 parent_fam_earn
	ren v11 kid_fam_earn
	ren v12 parent_pctile
	ren v13 kid_pctile
	
		egen state_num = group(state_move)
		keep if state_num == `i'
		replace weight = weight*1000000
		gen popweight = weight/5000

		//in-migration rate
		gen count = 1 //total population
		gen rawcount = 1/popweight
		replace popweight = round(popweight)
		collapse (sum) count rawcount [fw=popweight], by(state_move)
		ren count count_cfact
		ren rawcount rawcount_cfact
		save "$temp/pops_cfact_`i'", replace
}

clear
forval i = 1/50{
	append using "$temp/pops_cfact_`i'"
}
merge 1:1 state_move using "$temp/pops_baseline", keep(match) nogen
gen popchange = (count_cfact - count)/count_cfact * 100
su popchange


